<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Quanlytaikhoan extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 Thu
	 */
	function __construct()
    {
        parent::__construct();	
		$this->data['tieude'] = "Quản lý tài khoản";
		$this->loadadmin();
	} 
	
	public function index()
	{	
		$this->load->model('Taikhoan_model');
		$this->data['taikhoans'] = $this->Taikhoan_model->getresult_index();
		//print_r($this->data);
		$this->load->view('taikhoan/taikhoan', $this->data);
		$this->load->view('layout/footer');		
	}
	
	public function them()
	{
		$this->load->model('Taikhoan_model');
		$this->data['taikhoans'] = $this->Taikhoan_model->getresult_index();

		$this->load->view('taikhoan/themtaikhoan', $this->data);
		$this->load->view('layout/footer');
	}
	
	public function submit_them()
	{
		$this->load->library('form_validation');
		$this->load->model('Taikhoan_model');
		$this->data['taikhoans'] = $this->Taikhoan_model->getresult_index();
		
		if($this->form_validation->run('themtaikhoan') == false){
			$this->load->view('taikhoan/themtaikhoan', $this->data);
		}
		else
		{
			$this->data['thongtintaikhoan'] = $this->input->post();	
			//$this->data['taikhoan'] = $this->data['thongtintaikhoan']['matk'];
				
			$taikhoan = array(
							'tentaikhoan'=>$this->data['thongtintaikhoan']['tentaikhoan'],
							'matkhau'=>$this->data['thongtintaikhoan']['matkhau'],
							'hoten'=>$this->data['thongtintaikhoan']['hoten'],
							'diachi'=>$this->data['thongtintaikhoan']['diachi'],
							'sodienthoai'=>$this->data['thongtintaikhoan']['sodienthoai'],
							'email'=>$this->data['thongtintaikhoan']['email'],
							'trangthai'=>0,
							'quyen'=>1,
							);
							
			$this->data['taikhoan'] = $taikhoan;		
			$this->load->model('Taikhoan_model');
						
			$id = $this->Taikhoan_model->insert($this->data['taikhoan']);
				
			$this->data['thongbao'] = "Thêm thành công!".$this->data['thongtintaikhoan']['tentaikhoan']."!";	
		}
		$this->load->view('taikhoan/themtaikhoan',$this->data);
		$this->load->view('layout/footer');
	}
	
	public function sua($matk)
	{		
		$this->load->library('form_validation');
		$this->load->model('Taikhoan_model');
		
		$this->data['taikhoans'] = $this->Taikhoan_model->getresult_index();
				
		$this->data['thongtintaikhoan'] = $this->input->post();
		
		if(!empty($this->data['thongtintaikhoan']))
		{
			if($this->form_validation->run('themtaikhoan') )
			{
				//$this->data['thongtintaikhoan'] = $this->input->post();
				$this->Taikhoan_model->update($this->data['thongtintaikhoan'], $matk);
				$this->data['thongbao'] = "Sửa thành công!".$this->data['thongtintaikhoan']['tentaikhoan']."!";
			}	
		}
		
		$this->data['taikhoan'] = $this->Taikhoan_model->gettaikhoan($matk);
		
		$this->load->view('taikhoan/suataikhoan', $this->data);
		$this->load->view('layout/footer');
	}
	
	public function xoa($matk)
	{
		$this->load->model('Taikhoan_model');
		$this->Taikhoan_model->delete($matk);
		$dt= $this->input->get();
		
		if(!empty($dt['key']))	
			redirect("/Quanlytaikhoan/index/?key={$dt['key']}", 'refresh');
		else 
			redirect("/Quanlytaikhoan/index/", 'refresh');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */